import http from "@/axios/http";
import { json } from "stream/consumers";

// 统一的请求处理函数
const handleRequest = async (url, params) => {
    try {
        const res = await http.post(url, params);
        if (res.code === 200) {
            return res.data;
        }
        throw new Error(res.message || '请求失败');
    } catch (error) {
        console.error(`API请求错误: ${error.message}`, error);
        throw error;
    }
};

const teacherOperation = {
    // 获取用户信息
  selectAll: (params: any) => {
    return http
      .post('/TeachersController/findAllTeacher', params)
      .then((res) => {
        if (res.code == 200) {
          return res
        } else {
          throw new Error(res.message || 'FIND failed')
        }
      })
      .catch((error) => {
        // 统一处理错误
        console.error('Error during FIND:', error)
        throw error // 继续抛出错误，让调用方可以捕获
      })
  },
  selectAllT: (params: any) => {
    return http
      .post('/TeachersController/findAllTeacher', params)
      .then((res) => {
        if (res.code == 200) {
          return res
        } else {
          throw new Error(res.message || 'FIND failed')
        }
      })
      .catch((error) => {
        // 统一处理错误
        console.error('Error during FIND:', error)
        throw error // 继续抛出错误，让调用方可以捕获
      })
  },
    //
    // update: (params: any) => handleRequest(`${BASE_URL}/update`, params),
    // insert: (params: any) => handleRequest(`${BASE_URL}/insert`, params),
    // deletUser: (params: any) => handleRequest(`${BASE_URL}/deletUser`, params),
    // 删除用户
    deletUser: (params: any) => {
        return http.delete("/TeachersController/deleteTeacher", params).then(res=>{
            if(res.code == 200){
                return res.data;
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'DELET failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during DELET:', error);
            throw error;
        });
    },

    //编辑用户
    update: (params: any) => {
        return http.put("/TeachersController/updateTeacher", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'UPDATE failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during UPDATE:', error);
            throw error;
        });
    },

    //根据姓名查询老师信息
    findTeacherByName: (params: any) => {
        return http.get("/TeachersController/findTeacherByName", params).then(res=>{
            if(res.code == 200){
                return res.data;
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'FIND failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during FIND:', error);
            throw error;
        });
    },

    //上传头像
    uploadFile: (params: any) => {
      return http.post("/TeachersController/uploadFile", params).then(res=>{
          if(res.code == 200){
              return res.data;
          }else{
              // 登录失败，抛出错误以便外部catch捕获
              throw new Error(res.message || 'FIND failed');
          }
      }).catch(error=>{
          // 统一处理错误
          console.error('Error during FIND:', error);
          throw error;
      });
  },
  checkAdminCode:(params: any) => {
    return http.post("/TeachersController/checkAdminCode", params)
        .then(res => {
            if(res.code == 200){
                return res; // 登录成功，返回数据
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'Login failed');
            }
        })
        .catch(error => {
            // 统一处理错误
            console.error('Error during login:', error);
            throw error; // 继续抛出错误，让调用方可以捕获
        });
    },
    
    //根据id查询老师信息
    findTeacherById: (params: any) => {
        return http.get("/TeachersController/findTeacherById", params).then(res=>{
            if(res.code == 200){
                return res.data;
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'FIND failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during FIND:', error);
            throw error;
        });
    },

    //根据老师姓名更改老师身份role
    updateTeacherRole: (params: any) => {
        return http.put("/TeachersController/updateTeacherRole", params).then(res=>{
            if(res.code == 200){
                return res.data;
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'UPDATE failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during UPDATE:', error);
            throw error;
        });
    },
};

export default teacherOperation;